package org.example.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author zhengdp
 * @Description
 * @Date 2022/9/24 17:05
 * @Created by zhengdp
 */
@Slf4j(topic = "c.FileReader")
public class FileReader {
    /**
     * 读取文件流信息 ，会使线程进入阻塞状态（操作系统层面的阻塞状态）
     * @param fileName
     */
    public static void read(String fileName){
        int idx = fileName.lastIndexOf(File.separator);
        String shortName = fileName.substring(idx+1);
        try(FileInputStream in = new FileInputStream(fileName)){
            long start = System.currentTimeMillis();
            log.info("read [{}] start ...",shortName);
            byte[] buf = new byte[1024];
            int n = -1;
            do {
                n = in.read(buf);
            }while(n != -1);
            long end = System.currentTimeMillis();
            log.info("read [{}] end ... cost : {} ms",shortName,end-start);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
